package com.vidyu.datalayer.dataaccesscomponents.impl;

import com.vidyu.businesslayer.businessentities.Contract;
import com.vidyu.businesslayer.businessentities.Pair;
import com.vidyu.datalayer.dataaccesscomponents.ContractDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: eyalsa
 * Date: 28/02/12
 * Time: 14:38
 * Implementation Dao object for ContractorDao
 */

@SuppressWarnings({"unchecked", "UnusedDeclaration"})
@Repository("contractDao")
public class ContractDaoImpl extends BaseDaoImpl implements ContractDao {

    @Autowired
    private HibernateTemplate template;

    @Override
    @Transactional(readOnly = false, propagation = Propagation.NESTED)
    public long save(Contract contract){
        return ((Long) template.save(contract));
    }

    @Override
    public Contract findById(long id) {
        return template.get(Contract.class, id);
    }

    @Override
    public List<Contract> getAllContractTitles(){
        List<Contract> titles = this.findByNamedQuery("getAllContractTitles");
        if (titles.isEmpty()) {
            return null;
        }
        return titles;
    }
}
